package h0907.sort;

import java.util.Arrays;

/**
  * @description 选择排序
  * @author 不知名帅哥
  * @date 2024/9/7 20:23
  * @version 1.0
*/
public class SelectSort {
    public static void main(String[] args) {
        int[] nums = {4,7,2,3,4};
        selSort(nums);
        System.out.println(Arrays.toString(nums));
    }

    private static void selSort(int[] nums) {
        int n= nums.length;
        for (int i = 0; i < n; i++) {
            int minIndex=i;
            for (int j = i+1; j < n; j++) {
                if (nums[j]<nums[minIndex]){
                    minIndex=j;
                }
            }
            swap(nums,i,minIndex);
        }
    }

    private static void swap(int[] nums, int i, int minIndex) {
        int temp = nums[i];
        nums[i] = nums[minIndex];
        nums[minIndex] = temp;
    }
}
